package com.sxbbc.common.core.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxbbc.common.core.dto.jz.PropertiesVo;
import com.sxbbc.common.core.entity.jz.ProcDescribe;
import com.sxbbc.common.core.mapper.jz.ProcDescribeMapper;
import com.sxbbc.common.core.service.IProcDescribeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品描述附属表 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-05-24
 */
@Service
public class ProcDescribeServiceImpl extends ServiceImpl<ProcDescribeMapper, ProcDescribe> implements IProcDescribeService {

    @Override
    public List<PropertiesVo> getProcPropertiesList(String proCode) {
        ProcDescribe procDescribe = this.selectByProCode(proCode);
        List<PropertiesVo> list = new ArrayList<>();
        if (procDescribe != null) {
            if (StringUtils.isNotBlank(procDescribe.getAttributeDetails())) {
                JSONArray procArray = JSONArray.parseArray(procDescribe.getAttributeDetails());
                for (Object o : procArray) {
                    JSONObject proObject = (JSONObject) o;
                    PropertiesVo vo = new PropertiesVo();
                    vo.setName(proObject.getString("name"));
                    vo.setValue(proObject.getString("value"));
                    list.add(vo);
                }
            }
        }
        return list;
    }

    @Override
    public ProcDescribe selectByProCode(String proCode) {
        QueryWrapper<ProcDescribe> wrapper = new QueryWrapper<>();
        wrapper.eq("pro_code",proCode);
        return super.getOne(wrapper);
    }

    @Override
    public List<PropertiesVo> getProcSkuList(String proCode) {
        ProcDescribe describe = this.selectByProCode(proCode);
        List<PropertiesVo> list = new ArrayList<>();
        if (describe != null) {
            if (StringUtils.isNotBlank(describe.getSpecDetails())) {
                JSONArray skuArray = JSONArray.parseArray(describe.getSpecDetails());
                for (Object o : skuArray) {
                    JSONObject obj = (JSONObject) o;
                    PropertiesVo vo = new PropertiesVo();
                    vo.setName(obj.getString("name"));
                    JSONArray detailArray = JSONArray.parseArray(obj.getString("detail"));
                    List<String> detailList = new ArrayList<>();
                    for (Object d : detailArray) {
                        JSONObject dObj = (JSONObject) d;
                        detailList.add(dObj.getString("name"));
                        vo.setDetailList(detailList);
                    }
                    list.add(vo);
                }
            }
        }
        return list;
    }

    @Override
    public boolean updateByProCode(ProcDescribe procDescribe) {
        QueryWrapper<ProcDescribe> wrapper = new QueryWrapper<>();
        wrapper.eq("pro_code",procDescribe.getProCode());
        return super.update(procDescribe, wrapper);
    }
}
